<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/11
 * Time: 14:13
 */

namespace Home\Controller;


use Think\Controller;
use Overtrue\Wechat\Js;
class GuessController extends Controller
{
    public function index(){

        $openid =session('openid');
        if(empty($openid)){
           $this->openid();
        }
        $Guess = D('Guess');
        $ztr_num = $Guess->where("guess_name='ZTR'")->count();
        $num = $Guess->count();
        $ztr = round($ztr_num/$num,2)*100;
        $by = 100-$ztr;
        $this->assign('ztr',$ztr.'%');
        $this->assign('by',$by.'%');
        $s_openid = $Guess->where("openid='$openid'")->find();
        if(!$s_openid){
            $this->assign("is_guess","1");
        }
        if(IS_POST){
            $data  = $Guess->create();
            if($data){
                $phone = $data['phone'];
                if($s_openid){
                    $this->error('您已参与投票');
                }
                $s_phone =$Guess->where("phone='$phone'")->find();
                if($s_phone){
                    $this->error('该手机号码已参与投票');
                }
                $Guess->openid=session('openid');
                if($Guess->add()){
                    $this->success("恭喜您！投票成功");
                }
                else{
                    $this->error("失败");
                }
            }
            else{
                $this->error("失败");
            }
        }
        else{
            $this->assign('openid',$openid);
            $wechat = C('wechat');
            $jsapi = new Js($wechat['app'], $wechat['secret'] );
            $api = array('onMenuShareTimeline', 'onMenuShareAppMessage','onMenuShareQZone','onMenuShareQQ','onMenuShareWeibo');
            $this->assign('js',$jsapi->config($api,false));
            $this->display();
        }
    }
    public function openid()
    {
        if(empty($_GET['openid'])) {
            $auth_callback = 'http://'.$_SERVER['HTTP_HOST'].U('index');
            $auth_url = 'http://wxoauth.xuanzhen-tech.com/wx/auth?callback=' . urlencode($auth_callback);
            redirect($auth_url);
        }
        $openid = $_GET['openid'];
        $User = D('User');
        $data =  $User->where("openid='$openid'")->find();
        if(empty($data)){
            $userinfo = \httpGetJSON("http://wxoauth.xuanzhen-tech.com/wx/users/".$openid);
            $user = array(
                'openid'=>$openid,
                'oauthdata' => json_encode($userinfo),
            );
            $User->add($user);
        }
        session("openid",$openid);
        return $openid;

    }
    public function luckDraw($victory){
//        $guess =D('Guess')->count();
//        $this->assign('guess',$guess);
        $this->assign('victory',$victory);
        $this->display();
    }
    public function getList($victory){
        $suc=($victory==1)?'不与':'ZTR';
        $Guess =D('Guess');
        $data = $Guess
            ->join('lol_user on lol_user.openid=lol_guess.openid')
            ->field('lol_guess.id,lol_guess.phone,lol_user.oauthdata')
            ->where("is_guess!=1 && guess_name='$suc'")->order('rand()')->select();
        for($i=0;$i<count($data);$i++){
            $userdata = json_decode($data[$i]['oauthdata'],true);
            $data[$i]['nickname']=$userdata['nickname'];
            $data[$i]['headimgurl']=$userdata['headimgurl'];
            $data[$i]['oauthdata']=null;
        }
        $this->success($data);
    }
    public function luck($id){
        $Guess =D('Guess');
        $Guess->save(array(
            'id'=>$id,
            'is_guess'=>1,
        ));
        $this->success('成功');
    }
    public function phone(){
        $this->display();
    }

}